package com.wellsoft.mobileoa.utils.view;

import java.util.ArrayList;
import java.util.List;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.view.RadioView.WSOA_PageList;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListItem;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListSet;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListSubItem;

import android.R.integer;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class RadioViewAdapter extends BaseAdapter {
	public List<WSOA_PageList> pageLists;
	public Context context;
	public List<Integer> selectedIndex;

	public RadioViewAdapter(List<WSOA_PageList> pageLists, Context context) {
		this.pageLists = pageLists;
		this.context = context;
		selectedIndex = new ArrayList<Integer>();
	}

	@Override
	public int getCount() {
		if (pageLists == null) {
			return 0;
		} else {
			return pageLists.size();
		}
	}

	@Override
	public Object getItem(int arg0) {
		return pageLists.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		convertView = LayoutInflater.from(context).inflate(
				R.layout.radioviewitem, null);
		WSOA_PageListItem listItem = pageLists.get(position).getPageListItem();
		WSOA_PageListSubItem subItem = pageLists.get(position)
				.getPageListSubItem();
		RadioButton radioRb = (RadioButton) convertView
				.findViewById(R.id.radioviewitem_radiorb);
		TextView radioTv = (TextView)convertView.findViewById(R.id.radioviewitem_functv);
		radioRb.setOnClickListener(onRadioBoxSelectListener);
		radioRb.setTag(position);
		if (selectedIndex.contains(position)) {
			radioRb.setChecked(true);
		}

		radioTv.setText(listItem.getMessage() + '/' + subItem.getMessage());

		return convertView;
	}

	public List<String> getCheckedValues() {
		List<String> checkedValues = new ArrayList<String>();
		for (int i = 0; i < selectedIndex.size(); i++) {
			checkedValues.add(pageLists.get(selectedIndex.get(i))
					.getPageListSubItem().getValue());
		}
		return checkedValues;
	}

	public OnClickListener onRadioBoxSelectListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			RadioButton radioCb = (RadioButton) v;
			Integer position = (Integer) radioCb.getTag();
			if (!selectedIndex.contains(position)) {
				selectedIndex.clear();
				selectedIndex.add(position);
			}
			RadioViewAdapter.this.notifyDataSetChanged();
		}
	};

	public void selectItem(int position) {
		if(!selectedIndex.contains(position)){
			selectedIndex.clear();
			selectedIndex.add(position);
			RadioViewAdapter.this.notifyDataSetChanged();
		}
	}

}
